import base64
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
import json
import requests
import json
import cv2
import numpy as np
from PIL import Image
from io import BytesIO
import re
import time


def image_to_base64(image: Image.Image, fmt='png') -> str:
    output_buffer = BytesIO()
    image.save(output_buffer, format=fmt)
    byte_data = output_buffer.getvalue()
    base64_str = base64.b64encode(byte_data).decode('utf-8')
    return base64_str


def encode_image(filename):
    """
   编码图片
   :param filename: str 本地图片文件名
   :return: str 编码后的字符串
       eg:
       src="
           yH5BAAAAAAALAAAAAAzADEAAAK8jI+pBr0PowytzotTtbm/DTqQ6C3hGX
           ElcraA9jIr66ozVpM3nseUvYP1UEHF0FUUHkNJxhLZfEJNvol06tzwrgd
           LbXsFZYmSMPnHLB+zNJFbq15+SOf50+6rG7lKOjwV1ibGdhHYRVYVJ9Wn
           k2HWtLdIWMSH9lfyODZoZTb4xdnpxQSEF9oyOWIqp6gaI9pI1Qo7BijbF
           ZkoaAtEeiiLeKn72xM7vMZofJy8zJys2UxsCT3kO229LH1tXAAAOw=="

   """
    # 1、文件读取
    ext = filename.split(".")[-1]

    with open(filename, "rb") as f:
        img = f.read()
    img = img.con
    # 2、base64编码
    data = base64.b64encode(img).decode()

    # 3、图片编码字符串拼接

    return data


is_pre_enhance = 0
fundus_flag = 1
octa_flag = 0

# OCTA
if octa_flag:

    octa_path = 'tmp/nerve_seg/octa.png'
    img = Image.open(octa_path)
    img = img.convert("L")
    encoded_octa = image_to_base64(img)
else:
    encoded_octa = None
# Fundus
if fundus_flag:
    fundus_path = 'tmp/nerve_seg/fundus.png'
    img = Image.open(fundus_path)
    encoded_fundus = image_to_base64(img)
else:
    encoded_fundus = None
# serializer = Serializer(秘钥, 有效期秒)
# serializer = Serializer(settings.SECRET_KEY, 300)
# serializer.dumps(数据), 返回bytes类型
# token = serializer.dumps({'mobile': '18512345678'})
# token = token.decode()
json = {  # "token": token,
    "is_pre_enhance": is_pre_enhance,
    "fundus_img": encoded_fundus,
    "octa_img": encoded_octa
}

dr = None
hp = None
ad = "{'ad_class': None,'ad_prob': None,}"
# octa、fundus状态码
octa_fundus = octa_flag * 2 + fundus_flag

# 无图片
if (octa_flag or fundus_flag) == False:
    print('no pic')
# 有fundus
if (fundus_flag == 1) and (octa_flag == 0):
    enhance = requests.post("http://127.0.0.1:3000/enhance/", json=json)
    dr = requests.post("http://127.0.0.1:3000/dr_fundus/", json=json)
    hp = requests.post("http://127.0.0.1:3000/hp_fundus/", json=json)
# 有octa
if (fundus_flag == 0) and (octa_flag == 1):
    ad = requests.post("http://127.0.0.1:3000/ad/", json=json)
    dr = requests.post("http://127.0.0.1:3000/dr_octa33/", json=json)
    hp = requests.post("http://127.0.0.1:3000/hp_octa33/", json=json)
# muti2
if (fundus_flag == 1) and (octa_flag == 1):
    # print(json)
    ad = requests.post("http://127.0.0.1:3000/ad/", json=json)
    # time.sleep(10)
    dr = requests.post("http://127.0.0.1:3000/dr_multi2/", json=json)
    hp = requests.post("http://127.0.0.1:3000/hp_multi2/", json=json)

name = ['fundus', 'octa33', 'muti2']
find_class = '_clas": (.*?),'
find_prob = '_prob": (.*?),'
'''
return_json = {
    'dr_class': re.search(find_class, dr.text).group(1),
    'dr_prob': re.search(find_prob, dr.text).group(1),
    'hp_class': re.search(find_class, hp.text).group(1),
    'hp_prob': re.search(find_prob, hp.text).group(1),
    'ad_class': re.search(find_class, ad.text).group(1),
    'ad_prob': re.search(find_prob, ad.text).group(1),
}
print(return_json)
'''
